package app.manager;

import java.util.List; 
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import app.model.*;

public class BalanceManager implements UserBalancesDAO<Balance>{
	
	private String GET_All = "select b from Balance b";
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	@Transactional
	public int create(Balance balance) {
		sessionFactory.getCurrentSession().save(balance);
		return balance.getId();
	}

	@Override
	public Balance read(int id) {
		Balance balance = (Balance)sessionFactory.getCurrentSession().load(Balance.class,id);
		return balance;
	}

	@Override
	@Transactional
	public int update(Balance balance) {
		Balance blnc = (Balance)sessionFactory.getCurrentSession().load(Balance.class,balance.getId());
		
		if(blnc!= null){
			sessionFactory.getCurrentSession().update(balance);
			return blnc.getId();
		}
		return -1;
	}

	@Override
	@Transactional
	public void delete(int id) {
		Balance blnc = (Balance)sessionFactory.getCurrentSession().load(Balance.class,id);
		
		if(blnc!= null){
			sessionFactory.getCurrentSession().delete(blnc);
		}
	}

	@Override
	public List<Balance> getAll() {
		return sessionFactory.getCurrentSession().createQuery(GET_All).list();
	}
}
